var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
    clicksToEdit: 1
});
//票据录入明细grid
Ext.define('HIT.view.DealEnterGridPanel', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.dealentergridpanel',

    cls: 'custom-grid',
    frame: true,
    height: 210,
    columnLines: true,
    store: 'InvoiceDetail',
    id:'enter-invoice-grid',

    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            columns: [ {
                          xtype: 'rownumberer',
                          resizable: true,
                          width: 30
                      },{
						    xtype: 'gridcolumn',
						    width: 160,                                    
						    align: 'left',
						    maxLength:200,
						    style: 'text-align:center;font-weight:bold;',
						    dataIndex:'goods_name',
						    text: Share.T('货物名称'),
						    menuDisabled:true,
						    sortable: false,
						    listeners : {
						    	click : cellclick
							},
							renderer : render,
	                        summaryType: function(records){
	                            return "<span style='font-size:12px;font-weight:bold'>"+Share.T("合计")+"</span>";
	                        },
							editor: null
                      }, {
                          xtype: 'gridcolumn',
                          align: 'center',
                          maxLength:20,
                          style: 'font-weight:bold',
                          text: Share.T('商品税目'),
                          dataIndex:'goods_tax',
                          menuDisabled:true,
                          sortable: false,
//                          summaryType: function(records){
//                          	return "<span style='font-size:12px;font-weight:bold'>"+Share.T("合计")+"</span>";
//                          },
                          listeners : {
                          	click : cellclick
          				},
          				editor: null
          				,
          				renderer : render
                      }, {
						    xtype: 'gridcolumn',                                  
						    align: 'center',
						    style: 'text-align:center;font-weight:bold;',
						    text: Share.T('规格'),
						    maxLength:100,
						    dataIndex:'specification',
						    menuDisabled:true,
						    sortable: false,
						    listeners : {
						    	click : cellclick
							},
							renderer :render,
							editor: null  
                      }, {
                    	    xtype: 'gridcolumn',
				                text     : Share.T('计价单位'),
				                dataIndex:'unit',
				                style: 'text-align:center;font-weight:bold;',
				                maxLength:20,
				                align: 'center',
				                menuDisabled:true,
	                            sortable: false,
	                            listeners : {
	                            	click : cellclick
	            				},
	            				renderer : render
                      }, {
	                     	xtype: 'gridcolumn',				                    	
			                text     : Share.T('数量'),
			                dataIndex:'quantity',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                maxValue:100000000,
							minValue:0.000001,
			                menuDisabled:true,
	                        sortable: false,
	                        renderer: render,
	                        listeners : {
	                        	click : cellclick
	        				},
	        				editor: null
                      }, {
			             	xtype: 'gridcolumn',
			             	renderer: Ext.util.Format.numberRenderer('000,000,000.00'),
			                text     : Share.T('单价'),
			                dataIndex:'price',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        renderer:render,
	                        listeners : {
	                        	click : cellclick
	        				},
	        				editor: null
                      }, {
		        	        xtype:'gridcolumn',
		        	        renderer: Ext.util.Format.numberRenderer('000,000,000.00'),
						    width: 100,                                    
						    align: 'center',
						    style: 'text-align:center;font-weight:bold;',
						    text: Share.T('金额'),
						    dataIndex:'amount',
						    menuDisabled:true,
						    sortable: false,
						    summaryType: function(records){//合计
			                    var i = 0,
			                        length = records.length,
			                        total = 0,
			                        SA = 0,//合计不含税金额
			                        X = 1,//计价方式为0（不含税）时：1，计价方式为1时：1+R
			                        record;					                    
			                    for (; i < length; ++i) {
			                        record = records[i];					                       
			                        if(record.get('amount')!=''&&record.get('amount')!=null){
			                        	total += parseFloat(record.get('amount'));
			                        }					                        
			                        if(CountMode == 1){
				                    	X=1+parseFloat(record.get('rate'));
				                    }else{
				                    	X = 1;
				                    }
			                        var A = parseFloat(record.get('price'))*parseFloat(record.get('quantity'));
			                        var DA = A*(parseFloat(record.get('discount_rate'))/100);
			                        SA += ((A-DA)/X);
			                    }
			                    TotalAmount = Share.RetainDecimal(total);
			                    return "<span style='font-size:12px;font-weight:bold'>" + Share.fmoney(TotalAmount, 2) + "</span>";
			                },
			                renderer:render,      
						    listeners : {
						    	click : cellclick
							},
						    editor: null
                      }, {
							xtype:'gridcolumn',
						    width: 100,                                    
						    align: 'center',
						    style: 'text-align:center;font-weight:bold;',
						    text: Share.T('税率'),
						    dataIndex:'rate',
						    menuDisabled:true,
						    sortable: false,
						    renderer: render,
						    listeners : {
						    	click : cellclick
							},
							editor: null
                      }, {
                        	xtype:'gridcolumn',
			                text     : Share.T('税额'),
			                dataIndex:'tax',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
                            sortable: false,
                            width: 100,
                            summaryType: function(records){
			                    var i = 0,
			                        length = records.length,
			                        total = 0,
			                        ST = 0,//合计税额
			                        record;							
			                    for (; i < length; ++i) {
			                        record = records[i];
			                        if(record.get('tax')!=''&&record.get('tax')!=null){
			                        	total += parseFloat(record.get('tax'));				                        	
			                        }
			                        if(CountMode == 1){
				                    	X=1+parseFloat(record.get('rate'));
				                    }else{
				                    	X = 1;
				                    }
			                        var A = parseFloat(record.get('price'))*parseFloat(record.get('quantity'));
		                        	var T = A*parseFloat(record.get('rate'))/X;
		                        	var DT = A*(parseFloat(record.get('discount_rate'))/100)*parseFloat(record.get('rate'))/X;
		                        	ST += (T-DT);
			                    }
			                    TotalTax = Share.RetainDecimal(ST);
			                    if(total == 0){
			                    	TotalTax = 0.00;
			                    	return "<span style='font-size:12px;font-weight:bold'>0.00</span>";
			                    }else{                  	
			                    	return "<span style='font-size:12px;font-weight:bold'>" + Share.fmoney(total, 2) + "</span>";
			                    }
                            },
                            renderer: render,
                            listeners : {
                            	click : cellclick
            				},
            				editor: null
                      },{
              	    	xtype:'gridcolumn',
		                text     : Share.T('折扣率（％）'),
		                dataIndex:'discount_rate',
		                style: 'text-align:center;font-weight:bold;',
		                align: 'center',
		                menuDisabled:true,
                        sortable: false,
                        width: 100,
                        renderer: render,
                        listeners : {
                        	click : cellclick
        				},
        				editor: null
                      },{
                	    xtype: 'gridcolumn',
		                text     : Share.T('折扣金额'),
		                dataIndex:'discount_amount',
		                style: 'text-align:center;font-weight:bold;',
		                align: 'center',
		                menuDisabled:true,
                          sortable: false,
                          width: 100,
                          renderer: render,
                          listeners : {
                          	click : cellclick
        				    },
        				    editor: null
                      },{
              	        xtype:'gridcolumn',
			                text     : Share.T('折扣税额'),
			                dataIndex:'discount_tax',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
                          sortable: false,
                          width: 100,
                          renderer: render,
                          listeners : {
                        	 click : cellclick
        				   },
        				editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('计费里程'),
			                dataIndex:'tp_mileage',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-tp-mileage',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('货物的类别'),
			                dataIndex:'goods_category',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-goods-category',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('货物的重量'),
			                dataIndex:'weight',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-weight',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('货物的重量单位'),
			                dataIndex:'weight_unit',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-weight-unit',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('折算后重量'),
			                dataIndex:'weight_discount',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-weight-discount',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('重量单价'),
			                dataIndex:'weight_price',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-weight-price',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('货物体积'),
			                dataIndex:'volume',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-volume',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('货物体积单位'),
			                dataIndex:'volume_unit',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-volume-unit',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('体积单价'),
			                dataIndex:'volume_price',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-volume-price',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      },{
	              	        xtype:'gridcolumn',
			                text     : Share.T('运费金额'),
			                dataIndex:'fee_amount',
			                style: 'text-align:center;font-weight:bold;',
			                align: 'center',
			                menuDisabled:true,
	                        sortable: false,
	                        width: 100,
	                        renderer: render,
	                        id:'detail-fee-amount',
	                        listeners : {
	                      	 click : cellclick
	      				    },
	      				    editor: null
                      }    
                  ],
                  viewConfig: {
                    	enableTextSelection:true,
                  	    trackOver: false,
                        stripeRows: false
                  },
                  features: [{ftype: 'summary'}],
                  selModel: Ext.create('Ext.selection.CheckboxModel', {

                  }),
                  selType: 'cellmodel', 
                  loadMask: true, 
                  disableSelection: false,
                  plugins: [cellEditing]
        });
        
        /*背景淡黄色，原值返回*/
        function render(value, cellmeta, record, rowIndex, columnIndex, store) { 
            cellmeta.style='background-color: #FFFF80;';    			
    			return value;
        }         
        
        //当前编辑行 
        var Number=0;
        
      //货物名称       
        var goodsNameEditor = new Ext.form.ComboBox({
        	// --	GOODS_NAME	nvarchar(200)	商品中文名称
        	triggerAction : 'query',
        	editable : true,
            msgTarget:'side',
  			maxLength : 200,
        	valueField : 'name',
        	displayField : 'name',
        	allowBlank : false,
         	hideTrigger : true,  
        	typeAhead : false,
        	queryDelay : 700,
        	selectOnFocus : true,
        	fieldStyle:'background:#FFFF80;',       	
        	store : new Ext.data.JsonStore({
				proxy: {
	                type: 'ajax', 
	                url : ctx + '/getGoodsInfoForEnter',
	                params:{},
	                reader: {
	                    type: 'json', 
	                    totalProperty: '', 
	                    root: '' 
	                } 
				}, 
		        fields:['tax','name','spec','unit','code','rate','price'],
		        autoLoad:true 
        	}),
        	listeners : {
        		beforequery : function(e) {//过滤提示
        			var combo = e.combo;   
        			if(!e.forceAll){   
        				var value = e.query;   
        				combo.store.filterBy(function(record,id){
        					var text = record.get(combo.displayField); 
        					return (text.indexOf(value)!=-1);  
        				});
        				combo.expand();   
        				return false;   
        			}
        		},
        		select : function(combo, records, index) {
        			var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];
        			var num = grid.getStore().indexOf(recordtoedit);
        			cellEditing.startEditByPosition({row: num, column: 6}); 
        			var tax = records[0].raw.tax;
        			var spec = records[0].raw.spec;
        			var unit = records[0].raw.unit;
        			var code = records[0].raw.code;
        			var rate = records[0].raw.rate;
        			var	price = records[0].raw.price;
        			var	name = records[0].raw.name;
        			var start =name.indexOf('   ');
        			var newname=name.substring(start+3,name.length);
        			
        			recordtoedit.set('goods_name',newname);
 		        	recordtoedit.set('goods_tax',tax);
					recordtoedit.set('specification',spec);
					recordtoedit.set('unit',unit);
					recordtoedit.set('goods_code',code);
					recordtoedit.set('rate',rate);
					recordtoedit.set('price',price);
					
					var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit); 
//        			if(!/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,6})?$/.test(parseFloat(this.getValue()))){
//            			return;
//            		}
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getStore().getAt(Number);       			
        			var Q = parseFloat(recordtoedit.get('quantity'));
        			var P = parseFloat(recordtoedit.get('price'));
        			var R = parseFloat(recordtoedit.get('rate'));
        			var DR = parseFloat(recordtoedit.get('discount_rate'))/100;
        			var X = 1;
        			if(CountMode == 1){
 		        		X = 1+R;
 		        	}else{
 		        		X = 1;
 		        	}
        			cellEditing.startEditByPosition({row: Number, column: 13});//折扣税额
 		        	recordtoedit.set('amount',Share.RetainDecimal(Q*P));
 		        	
 		        	recordtoedit.set('tax',Share.RetainDecimal(Q*P*R/X));
 		        	
 		        	recordtoedit.set('discount_amount',Share.RetainDecimal(P*Q*DR));
 		         	recordtoedit.set('discount_tax',Share.RetainDecimal(P*Q*DR*R/X));
        		}
        	}
        });
        
 //商品税目       
        var goodsTaxEditor = new Ext.form.TextField({
        	// --	TAX_ITEM	nvarchar(20)	税目
        	msgTarget:'side',
  			maxLength : 20,
        	fieldStyle:'background:#FFFF80;',
        	listeners:{
        		focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);  
            	},
            	blur:function(){}
        	}
        });

        
//规格      
        var specificationEditor = new Ext.form.TextField({
        	// --	SPEC	nvarchar(100)	规格型号
        	allowBlank:false,
        	msgTarget:'side',
  			maxLength : 100,
         	blankText:Share.T('规格不能为空'),
        	fieldStyle:'background:#FFFF80;',
        	listeners:{
        		focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);  
            	},
            	blur:function(){}
        	}
        });
        
//计价单位      
        var unitEditor = new Ext.form.TextField({
        	// --	GOODS_UNIT	nvarchar(20)	商品单位
        	allowBlank:false,
        	msgTarget:'side',
  			maxLength : 20,
         	blankText:Share.T('计价单位不能为空'),
        	fieldStyle:'background:#FFFF80;',
        	listeners:{
        		focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);  
            	},
            	blur:function(){}
        	}
        });        


//数量
        var quantityEditor = new Ext.form.NumberField({
        	// --	QUANTITY	decimal(18, 6)	数量
        	allowBlank:false,
        	msgTarget:'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
         	blankText:Share.T('数量不能为空'),
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,6})?$/.test(value)){
            		if(value != 0){
        		    	return true;
             		}
        		}
        		return Share.T('数量输入有误'); 
            },
            listeners:{
            	focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);  
            	},
            	blur:function(compo,the,e){       
            		if(!/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,6})?$/.test(parseFloat(this.getValue()))){
            			return;
            		}
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getStore().getAt(Number);       			
        			var Q = parseFloat(this.getValue());
        			var P = parseFloat(recordtoedit.get('price'));
        			var R = parseFloat(recordtoedit.get('rate'));
        			var DR = parseFloat(recordtoedit.get('discount_rate'))/100;
        			var X = 1;
        			if(CountMode == 1){
 		        		X = 1+R;
 		        	}else{
 		        		X = 1;
 		        	}
        			cellEditing.startEditByPosition({row: Number, column: 13});//折扣税额
 		        	recordtoedit.set('amount',Share.RetainDecimal(Q*P));
 		        	
 		        	recordtoedit.set('tax',Share.RetainDecimal(Q*P*R/X));
 		        	
 		        	recordtoedit.set('discount_amount',Share.RetainDecimal(P*Q*DR));
 		         	recordtoedit.set('discount_tax',Share.RetainDecimal(P*Q*DR*R/X));
            	}
            }
        });
//单价
        var priceEditor = new Ext.form.NumberField({
        	// --	PRICE_CUR	decimal(18, 6)	本币单价
        	allowBlank:false,
        	msgTarget:'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
         	blankText:Share.T('单价不能为空'),
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
             	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,6})?$/.test(value)){
             		if(value != 0){
        		    	return true;
             		}
        		}
        		return Share.T('单价输入有误'); 
            },listeners:{
            	focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);       			
            	},
            	blur:function(){
            		if(!/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,6})?$/.test(parseFloat(this.getValue()))){
            			return;
            		}
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getStore().getAt(Number);       			
        			var Q = parseFloat(recordtoedit.get('quantity'));
        			var P = parseFloat(this.getValue());
        			var R = parseFloat(recordtoedit.get('rate'));
        			var DR = parseFloat(recordtoedit.get('discount_rate'))/100;
        			var X = 1;
        			if(CountMode == 1){
 		        		X = 1+R;
 		        	}else{
 		        		X = 1;
 		        	}
        			cellEditing.startEditByPosition({row: Number, column:13});//折扣税额
 		        	recordtoedit.set('amount',Share.RetainDecimal(Q*P));
 		        	
 		        	recordtoedit.set('tax',Share.RetainDecimal(Q*P*R/X));
 		        	recordtoedit.set('discount_amount',Share.RetainDecimal(P*Q*DR));
 		        	recordtoedit.set('discount_tax',Share.RetainDecimal(P*Q*DR*R/X));
            	}
            }
        });
        
 //金额       
        var amountEditor = new Ext.form.NumberField({
        	// --	AMOUNT_CUR	decimal(18, 2)	本币金额
        	allowBlank:false,
        	msgTarget:'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :2,
			hideTrigger:true,
			allowDecimals:true,
         	blankText:Share.T('金额不能为空'),
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(value)){
        			return true;
        		}
            	else{
        			return Share.T('金额输入有误');
        		}
            },listeners:{
            	focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);       			
            	},
            	blur:function(){
            		if(!/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(parseFloat(this.getValue()))){
            			return;
            		}
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getStore().getAt(Number);       			
        			var Q = parseFloat(recordtoedit.get('quantity'));
        			var A = parseFloat(this.getValue());
        			var R = parseFloat(recordtoedit.get('rate'));
        			var DR = parseFloat(recordtoedit.get('discount_rate'))/100;
        			var X = 1;
        			if(CountMode == 1){
 		        		X = 1+R;
 		        	}else{
 		        		X = 1;
 		        	}
        			cellEditing.startEditByPosition({row: Number, column: 13});//折扣税额
        			recordtoedit.set('price',Share.RetainDecimal(A/Q,6));
        			recordtoedit.set('tax',Share.RetainDecimal(A*R/X)); 		        	
 		        	recordtoedit.set('discount_amount',Share.RetainDecimal(A*DR));
 		        	recordtoedit.set('discount_tax',Share.RetainDecimal(A*DR*R/X));
            	}
            }
        });
        
//税率      
        var rateEditor = new Ext.form.NumberField({
        	// --	TAX_RATE	decimal(18, 6)	税率
        	allowBlank:false,
        	msgTarget:'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
         	blankText:Share.T('税率不能为空'),
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(/^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(value)){
        			return true;
        		}
        		return Share.T('税率输入有误'); 
            },listeners:{
            	focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);       			
            	},
            	blur:function(){
            		if(!/^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(parseFloat(this.getValue()))){
            			return;
            		}
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getStore().getAt(Number);  
        			var A = parseFloat(recordtoedit.get('amount'));
        			var R = parseFloat(this.getValue());
        			var DA = parseFloat(recordtoedit.get('discount_amount'));
        			var X = 1;
        			if(CountMode == 1){
 		        		X = 1+R;
 		        	}else{
 		        		X = 1;
 		        	}
        			cellEditing.startEditByPosition({row: Number, column: 13});//折扣税额 
 		        	recordtoedit.set('tax',Share.RetainDecimal(A*R/X));
 		        	recordtoedit.set('discount_tax',Share.RetainDecimal(DA*R/X));
 		        	recordtoedit.set('rate',R);
            	}
            }
        });
        
 //税额     
        var taxEditor = new Ext.form.NumberField({
        	// --	TAX_AMOUNT	decimal(18, 2)	税额
        	allowBlank:false,
        	msgTarget:'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :2,
			hideTrigger:true,
			allowDecimals:true,
         	blankText:Share.T('税额不能为空'),
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(value)){
        			return true;
        		}
        		return Share.T('税额输入有误'); 
            }
        });

//折扣率      
        var disRateEditor = new Ext.form.NumberField({
        	// --	DISCOUNT_RATE	decimal(18, 6)	折扣率
        	allowBlank:false,
        	msgTarget:'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
         	blankText:Share.T('折扣率不能为空'),
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(/^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(value)){
        			return true;
        		}
        		return Share.T('折扣率输入有误'); 
            },listeners:{
            	focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);       			
            	},
            	blur:function(){
            		if(!/^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(parseFloat(this.getValue()))){
            			return;
            		}
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getStore().getAt(Number); 
        			var A = parseFloat(recordtoedit.get('amount'));
        			var R = parseFloat(recordtoedit.get('rate'));
        			var DR = parseFloat(this.getValue())/100;
        			var X = 1;
        			if(CountMode == 1){
 		        		X = 1+R;
 		        	}else{
 		        		X = 1;
 		        	}
        			cellEditing.startEditByPosition({row: Number, column: 13});//折扣税额
        			recordtoedit.set('discount_amount',Share.RetainDecimal(A*DR));
 		        	recordtoedit.set('discount_tax',Share.RetainDecimal(A*DR*R/X));
 		        	recordtoedit.set('discount_rate',DR*100);//如将“01”格式自动转换为“1”
            	}
            }
        });
        
//折扣金额     
        var disAmountEditor = new Ext.form.NumberField({
        	// --	DISCOUNT_SUM	decimal(18, 2)	折扣金额
        	allowBlank:false,
        	msgTarget:'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :2,
			hideTrigger:true,
			allowDecimals:true,
         	blankText:Share.T('折扣金额不能为空'),
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(value)){
        			return true;
        		}
        		return Share.T('折扣金额输入有误'); 
            },
            listeners:{
            	focus:function(){
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getSelectionModel().getSelection()[0];  
        			Number = grid.getStore().indexOf(recordtoedit);       			
            	},
            	blur:function(){
            		if(!/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(parseFloat(this.getValue()))){
            			return;
            		}
            		var grid = Ext.getCmp('enter-invoice-grid');
        			var recordtoedit = grid.getStore().getAt(Number); 
        			var R = parseFloat(recordtoedit.get('rate'));
        			var DA = parseFloat(this.getValue());
        			var A = parseFloat(recordtoedit.get('amount'));
        			var X = 1;
        			if(CountMode == 1){
 		        		X = 1+R;
 		        	}else{
 		        		X = 1;
 		        	}
        			cellEditing.startEditByPosition({row: Number, column: 13});//折扣税额
 		        	recordtoedit.set('discount_tax',Share.RetainDecimal(DA*R/X));
 		        	recordtoedit.set('discount_rate',Share.RetainDecimal(DA*100/A,3));
            	}
            }
        });
        
//折扣税额     
        var disTaxEditor = new Ext.form.NumberField({
        	// --	DISCOUNT_TAX	decimal(18, 2)	折扣税额
        	allowBlank:false,
        	msgTarget:'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :2,
			hideTrigger:true,
			allowDecimals:true,
         	blankText:Share.T('折扣税额不能为空'),
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(value)){
        			return true;
        		}
        		return Share.T('折扣税额输入有误'); 
            }
        });
        
//计费里程    
        var tpMileageEditor = new Ext.form.NumberField({
        	// --	TP_MILEAGE	decimal(18, 6)	计费里程
            msgTarget  : 'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(value>=0){
        			return true;
        		}
        		return Share.T('计费里程输入有误'); 
            }
        });
//货物的类别
        var goodsCategoryEditor = new Ext.form.TextField({
        	// --	GOODS_CATEGORY	nvarchar(20)	货物的类别
            msgTarget  : 'side',
  			maxLength : 20,
            fieldStyle:'background:#FFFF80;'
        });  
//货物的重量   
        var weightEditor = new Ext.form.NumberField({
        	// --	WEIGHT	decimal(18, 6)	货物的重量
            msgTarget  : 'side',
			maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(value>=0){
        			return true;
        		}
        		return Share.T('货物重量输入有误'); 
            }
        });         
//货物的重量单位
        var weightUnitEditor = new Ext.form.TextField({
        	// --	WEIGHT_UNIT	nvarchar(20)	货物的重量单位
            msgTarget  : 'side',
            maxLength : 20,
            fieldStyle:'background:#FFFF80;'
        });  
//折算后重量    
        var weightDiscountEditor = new Ext.form.NumberField({
        	// --	WEIGHT_DISCOUNT	decimal(18, 6)	折算后重量
            msgTarget  : 'side',
            maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(value>=0){
        			return true;
        		}
        		return Share.T('折算后重量输入有误'); 
            }
        });
//重量单价
        var weightPriceEditor = new Ext.form.NumberField({
        	// --	WEIGHT_PRICE	decimal(18, 6)	重量单价
            msgTarget  : 'side',
            maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(value>=0){
        			return true;
        		}
        		return Share.T('重量单价输入有误'); 
            }
        });  
//货物体积    
        var volumeEditor = new Ext.form.NumberField({
        	// --	VOLUME	decimal(18, 6)	货物体积
            msgTarget  : 'side',
            maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(value>=0){
        			return true;
        		}
        		return Share.T('货物体积输入有误'); 
            }
            
        });        
//货物体积单位    
        var volumeUnitEditor = new Ext.form.TextField({
        	// --	VOLUME_UNIT	nvarchar(20)	货物体积单位
            msgTarget  : 'side',
            maxLength : 20,
            fieldStyle:'background:#FFFF80;'
        });
//体积单价
        var volumePriceEditor = new Ext.form.NumberField({
        	// --	VOLUME_PRICE	decimal(18, 6)	体积单价
            msgTarget  : 'side',
            maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(value>=0){
        			return true;
        		}
        		return Share.T('体积单价输入有误'); 
            }
        });  
//运费金额   
        var feeAmountEditor = new Ext.form.NumberField({
        	// --	FEE_AMOUNT	decimal(18, 6)	运费金额
            msgTarget  : 'side',
            maxValue:10000000000,
			minValue:-10000000000,
			decimalPrecision :6,
			hideTrigger:true,
			allowDecimals:true,
            fieldStyle:'background:#FFFF80;',
            validator : function(value) {
            	if(value>=0){
        			return true;
        		}
        		return Share.T('运费金额输入有误'); 
            }
        });         
        
        
        Ext.apply(Ext.form.VTypes, {
    		 moneyrange: function(val, field) {
    			if(val == 0){
             		return false;
             	}
    			if(/^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(val)){
    				return true;
    			}
    			return false;
    	     },
    	     moneyrangeText: Share.T('折扣税额输入有误')
        });
        
        /*单击grid单元格切换为编辑状态*/
        function cellclick(value, cellmeta, record, rowIndex, columnIndex, store){
        	cellEditing.cancelEdit();
        	var obj = this;
        	var bar = Ext.getCmp('actionbar');
        	var mark = 0;//判断是否在编辑状态，1：编辑状态
        	bar.items.each(function(item) {
                item.items.each(function(subItem) {
                    if (subItem.id == 'deal-invoice-save') {
                    	mark = 1;
                    	if(subItem.disabled){
                    		 if(obj.editor !=undefined || obj.editor !=null){
                    		  obj.editor = null; 
                    		  }
                         	  obj.field = null;
                    	}else{
                    		switch(rowIndex){
                    		case 2:
                		    	obj.editor = goodsNameEditor; 
                				break;
                			case 3:       
                	            obj.editor = goodsTaxEditor;
                				break;
                			case 4:
                                obj.editor = specificationEditor;
                				break;
                			case 5:
                                obj.editor = unitEditor;
                				break;
                			case 6:
                		 		obj.editor = quantityEditor;
                				break;
                			case 7:
                		 		obj.editor = priceEditor;
                				break;
                			case 8:
                		    	obj.editor = amountEditor;
                				break;
                			case 9:
                		 		obj.editor = rateEditor;
                				break;
                			case 10:
                		 		obj.editor = taxEditor; 
                				break;
                			case 11:
                		 		obj.editor = disRateEditor; 
                				break;
                			case 12:
                		 		obj.editor = disAmountEditor; 
                				break;
                			case 13:
                		 		obj.editor = disTaxEditor; 
                				break;
                			case 14:       
                	            obj.editor = tpMileageEditor;  
                				break;
                			case 15:
                                obj.editor = goodsCategoryEditor;
                				break;
                			case 16:
                                obj.editor = weightEditor;
                				break;
                			case 17:
                		 		obj.editor = weightUnitEditor;
                				break;
                			case 18:
                		 		obj.editor = weightDiscountEditor;
                				break;
                			case 19:
                		    	obj.editor = weightPriceEditor;
                				break;
                			case 20:
                		 		obj.editor = volumeEditor;
                				break;
                			case 21:
                		 		obj.editor = volumeUnitEditor; 
                				break;
                			case 22:
                		 		obj.editor = volumePriceEditor; 
                				break;
                			case 23:
                		 		obj.editor = feeAmountEditor; 
                				break;
                    	}
                      }
                    }
                 });
            });

        	if(mark == 0){
        		if(obj.editor !=undefined || obj.editor !=null){
          		  obj.editor = null; 
          		  }
               	  obj.field = null;
        	}
        }
        me.callParent(arguments);
    }

});